package com.wxmit.ot.base;

import com.wxmit.ot.base.constant.HttpStatus;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description ApiResult
 * @ignoreResponseBodyAdvice
 * @Author huangxiong
 * @Date 2022/7/11 14:53
 * @Version V1.0
 */
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String SUCCESS = "success";
    public static final String FAIL = "fail";


    private int code;
    private T data;
    private String msg;

    public ApiResult(){}

    public ApiResult(int code,T data,String msg){
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static <T> ApiResult<T> success(){
        return success(null);
    }
    public static <T> ApiResult<T> success(String msg){
        return success(null,msg);
    }
    public static <T> ApiResult<T> successWithData(T data){
        return success(data,SUCCESS);
    }
    public static <T> ApiResult<T> success(T data,String msg){
        return new ApiResult<>(HttpStatus.SUCCESS,data,msg);
    }


    public static <T> ApiResult<T> fail(){
        return fail(FAIL);
    }
    public static <T> ApiResult<T> fail(String msg){
        return fail(null,msg);
    }
    public static <T> ApiResult<T> fail(T data,String msg){
        return new ApiResult<>(HttpStatus.ERROR,data,msg);
    }
    public static <T> ApiResult<T> failWithCode(int code,String msg){
        return build(code,null,msg);
    }

    public static  <T> ApiResult<T> build(int code,T data,String msg){
        return new ApiResult<T>(code,data,msg);
    }
}
